package com.tencent.product.controller;

import com.tencent.product.service.IProductCommentService;
import com.tencent.product.domain.ProductComment;
import com.tencent.product.query.ProductCommentQuery;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/productComment")
public class ProductCommentController {

    @Autowired
    public IProductCommentService productCommentService;

    /**
     * 接口：添加或修改
     * @param productComment  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody ProductComment productComment){
        try {
            if( productComment.getId()!=null)
                productCommentService.update(productComment);
            else
                productCommentService.save(productComment);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productCommentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
                productCommentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后再试！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public ProductComment findOne(@PathVariable("id")Long id) {
        return productCommentService.queryById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<ProductComment> findAll(){
        return productCommentService.queryAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<ProductComment> queryPage(@RequestBody ProductCommentQuery query) {
        return productCommentService.queryPage(query);
    }
}
